package exec;

import java.util.Arrays;
import java.util.Random;

public class TWSort {

    public static void main(String[] args) {
        // 二维数组排序
        String[] names = {"安琪拉", "王昭君", "蔡文姬", "妲己", "张良"};
        String[] courses = {"C++", "Java", "Python"};
        int[][] scores = {
                {90, 89, 75},
                {59, 40, 100},
                {100, 99, 80},
                {80, 61, 61},
                {60, 100, 99},
        };
        sortName(scores, names);
//        int[][] sort = sortByJavaScore(scores);
//        for (int[] ints : sort) {
//            System.out.println(Arrays.toString(ints));
//        }
    }

    // 定义一个方法，让数组按照java成绩排序
    public static int[][] sortByJavaScore(int[][] scores) {
        for (int i = 0; i < scores.length - 1; i++) { // 排序的次数
            for (int j = 0; j < scores.length - 1 - i; j++) {
                if (scores[j][1] < scores[j + 1][1]) {
                    int[] temp = scores[j];
                    scores[j] = scores[j + 1];
                    scores[j + 1] = temp;
                }
            }
        }
        return scores;
    }

    // 定义一个方法，让scores按照java成绩排序，并输出对应的学生名称
    public static void sortName(int[][] scores, String[] names) {
        for (int i = 0; i < scores.length - 1; i++) { // 排序的次数
            for (int j = 0; j < scores.length - 1 - i; j++) {
                if (scores[j][1] < scores[j + 1][1]) {
                    int[] temp = scores[j];
                    scores[j] = scores[j + 1];
                    scores[j + 1] = temp;
                    String t = names[j];
                    names[j] = names[j + 1];
                    names[j + 1] = t;
                }
            }
        }
        for (int i = 0; i < names.length; i++) {
            System.out.print("姓名：" + names[i]);
            System.out.print("，成绩：" + Arrays.toString(scores[i]));
            System.out.println();
        }
    }

}
